
(function() {
  "use strict";
  //加载用户信息
  var user;
  try {
    user = JSON.parse(sessionStorage.getItem("user"));
  } catch (e) {
    $.isLoading({
      text: "非法操作,请重新登录"
    });
    setTimeout(function() {
      window.location.href = "/login.html";
      $.isLoading("hide");
    }, 2000);
    return;
  }
  //  ---------  界面初始化 -----------
  $("html").niceScroll({
    styler: "fb",
    cursorcolor: "#65cea7",
    cursorwidth: '6',
    cursorborderradius: '0px',
    background: '#424f63',
    spacebarenabled: false,
    cursorborder: '0',
    zindex: '1000'
  });

  $(".left-side").niceScroll({
    styler: "fb",
    cursorcolor: "#65cea7",
    cursorwidth: '3',
    cursorborderradius: '0px',
    background: '#424f63',
    spacebarenabled: false,
    cursorborder: '0'
  });
  $(".left-side").getNiceScroll();
  if ($('body').hasClass('left-side-collapsed')) {
    $(".left-side").getNiceScroll().hide();
  }
  // Toggle Left Menu
  $(document).on("click", ".menu-list > a", function() {
    var parent = $(this).parent();
    var sub = parent.find('> ul');
    if (!$('body').hasClass('left-side-collapsed')) {
      if (sub.is(':visible')) {
        sub.slideUp(200, function() {
          parent.removeClass('nav-active');
          $('.main-content').css({
            height: ''
          });
          mainContentHeightAdjust();
        });
      } else {
        visibleSubMenuClose();
        parent.addClass('nav-active');
        sub.slideDown(200, function() {
          mainContentHeightAdjust();
        });
      }
    }
    return false;
  });

  function visibleSubMenuClose() {
    $('.menu-list').each(function() {
      var t = $(this);
      if (t.hasClass('nav-active')) {
        t.find('> ul').slideUp(200, function() {
          t.removeClass('nav-active');
        });
      }
    });
  }
  function mainContentHeightAdjust() {
    // Adjust main content height
    var docHeight = $(document).height();
    if (docHeight > $('.main-content').height())
      $('.main-content').height(docHeight);
  }

  //  class add mouse hover
  $(document).on("mouseover", ".custom-nav > li", function() {
    $(this).addClass('nav-hover');
  });
  $(document).on("mouseout", ".custom-nav > li", function() {
    $(this).removeClass('nav-hover');
  });
  // Menu Toggle
  $(document).on("click", ".toggle-btn", function() {
    $(".left-side").getNiceScroll().hide();

    if ($('body').hasClass('left-side-collapsed')) {
      $(".left-side").getNiceScroll().hide();
    }
    var body = $('body');
    var bodyposition = body.css('position');

    if (bodyposition != 'relative') {

      if (!body.hasClass('left-side-collapsed')) {
        body.addClass('left-side-collapsed');
        $('.custom-nav ul').attr('style', '');

        $(this).addClass('menu-collapsed');

      } else {
        body.removeClass('left-side-collapsed chat-view');
        $('.custom-nav li.active ul').css({
          display: 'block'
        });

        $(this).removeClass('menu-collapsed');

      }
    } else {

      if (body.hasClass('left-side-show'))
        body.removeClass('left-side-show');
      else
        body.addClass('left-side-show');

      mainContentHeightAdjust();
    }
  });
  searchform_reposition();
  $(window).resize(function() {
    if ($('body').css('position') == 'relative') {
      $('body').removeClass('left-side-collapsed');
    } else {
      $('body').css({
        left: '',
        marginRight: ''
      });
    }
    searchform_reposition();
  });
  function searchform_reposition() {
    if ($('.searchform').css('position') == 'relative') {
      $('.searchform').insertBefore('.left-side-inner .logged-user');
    } else {
      $('.searchform').insertBefore('.menu-right');
    }
  }
  // panel collapsible
  $('.panel .tools .fa').click(function() {
    var el = $(this).parents(".panel").children(".panel-body");
    if ($(this).hasClass("fa-chevron-down")) {
      $(this).removeClass("fa-chevron-down").addClass("fa-chevron-up");
      el.slideUp(200);
    } else {
      $(this).removeClass("fa-chevron-up").addClass("fa-chevron-down");
      el.slideDown(200);
    }
  });

  $(document).on('click', '.todo-check label', function() {
    $(this).parents('li').children('.todo-title').toggleClass('line-through');
  });
  $(document).on('click', '.todo-remove', function() {
    $(this).closest("li").remove();
    return false;
  });

  $("#sortable-todo").sortable();


  // panel close
  $('.panel .tools .fa-times').click(function() {
    $(this).parents(".panel").parent().remove();
  });
  //  ---------  界面初始化 结束 -----------
  //注销
  $(document).on("click", ".logout", function() {
    myHttp.put("logout/",function(){
      mygritter("注销成功");
      window.location.href = "/login.html";
    },function(){
      mygritter("注销失败");
    });
  });
  //修改密码
  $(document).on("click", ".changePwd", function() {
    window.location.href = "/user/changePwd.html#user-changepwd";
  });
  /**
   * 加载menu
   */
  if (!user) {
    $.isLoading({
      text: "您还未登录,请去登录"
    });
    setTimeout(function() {
      window.location.href = "/login.html";
      $.isLoading("hide");
    }, 2000);
  } else {
    $(".username").html(user.username);
    //获取目录
    myHttp.base("../json/" + user.type + ".json", function(data) {
      var menu = $(".custom-nav");
      var string = "";
      for (var i = 0; i < data.length; i++) {
        var item = data[i];
        if (item.sub != null && item.sub.length > 0) {
          string += '<li class="menu-list"><a href="#"><i class="fa ' + item.icon + '"></i> <span>' + item.name + '</span></a>';
          string += '<ul class="sub-menu-list">';
          for (var j = 0; j < item.sub.length; j++) {
            var sub = item.sub[j];
            string += '<li><a href="/' + item.key + '/' + sub.html + '#' + sub.hash + '" data-hash="#' + sub.hash + '"> ' + sub.name + '</a></li>';
          }
          string += '</ul>';
        } else {
          string += '<li><a href="/' + item.key + '/' + item.html + '#' + item.hash + '" data-hash="#' + item.hash + '"><i class="fa ' + item.icon + '"></i> <span>' + item.name + '</span></a></li>';
        }
        string += '</li>';
      }
      menu.html(string);
      // tool tips

      $('.tooltips').tooltip();

      // popovers

      $('.popovers').popover();
      $('body').find("a[data-hash='" + location.hash + "']").parent().addClass("active");
      $('body').find("a[data-hash='" + location.hash + "']").parents('.menu-list').addClass("nav-active");
    }, function() {
      $.isLoading({
        text: "非法操作,请重新登录"
      });
      setTimeout(function() {
        window.location.href = "/login.html";
        $.isLoading("hide");
      }, 2000);
    });
  }



  // ---------- 下拉框  ----------
  $(".select-chosen").chosen();
  //填充App应用下拉框
  if($("select.app_id").length > 0){
    myHttp.getJSON('api/app',function(data){
        var string="";
        for(var i=0;i<data.length;i++){
          var item = data[i];
          var id = item.id;
          var name = item.app_store_name_with_source;
          string += '<option value="'+id+'">'+name+'</option>';
        }
        $("select.app_id").append(string);
        $("select.app_id").chosen();
    });
  }




  // ----------- 时间控件 -------
  //时间初始化
  $("input.form_date").datepicker({
      format: 'yyyy-mm-dd'
  });
})($);
